using Admin3000.Backend.Api.Entity;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Admin3000.Backend.Api.Database;
using Microsoft.EntityFrameworkCore;
using System;

namespace Admin3000.Backend.Api.Repository
{
    public class EfRepository<T> : IRepository<T> where T : BaseEntity
    {
        private Admin3000Db _db;

        public EfRepository(Admin3000Db db)
        {
            _db = db;
        }

        //DbSet:表示可用于创建、读取、更新和删除操作的实体集
        private DbSet<T> _entity;

        protected DbSet<T> Entity
        {
            get
            {
                if (_entity == null)
                {
                    _entity = _db.Set<T>();
                }
                return _entity;
            }
        }

        public IQueryable<T> Table
        {
            get
            {
                return Entity.AsQueryable<T>();
            }
        }
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
        public void Delete(object id)
        {
            //通过Id找到要删除的Id
            var t = Entity.Find(id);
            if (t == null)
            {
                throw new ArgumentNullException(nameof(t));
            }
            //删除
            _db.Remove(t);
            //保存
            _db.SaveChanges();
        }

/// <summary>
/// 批量删除
/// </summary>
/// <param name="ids"></param>
        public void DeleteBulk(IEnumerable<object> ids)
        {
            var ii = new List<int>();
            //遍历所有的Id把要删除的添加到定义的空集合
            foreach (var item in ids)
            {
                var tmp = (int)item;
                ii.Add(tmp);
            }
            //contains:查询条件判断
            var ts = Entity.Where(x => ii.Contains(x.Id)).ToList();
            _db.RemoveRange(ts);
            _db.SaveChanges();
        }
/// <summary>
/// 通过Id查找
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
        public T GetById(object id)
        {
            return Entity.Find(id);
        }



/// <summary>
/// 插入
/// </summary>
/// <param name="entity"></param>
        public void Insert(T entity)
        {
            //判断提供的实体对象是否为空
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            //插入数据
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
            entity.DisplayOrder = 0;

            Entity.Add(entity);
            _db.SaveChanges();
        }

/// <summary>
/// 异步插入
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
        public async Task InsertAsync(T entity)
        {
            //判断提供的实体对象是否为空
            if (entity == null)
            {
                //如果实体对象为null，则抛出异常
                throw new ArgumentNullException();
            }


            //插入数据
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
            entity.DisplayOrder = 0;

            await Entity.AddAsync(entity);
            await _db.SaveChangesAsync();
        }


/// <summary>
/// 批量插入
/// </summary>
/// <param name="entities"></param>
        public void InsertBulk(IEnumerable<T> entities)
        {
            //遍历提供的实体对象集合
            foreach (var entity in entities)
            {
                entity.IsActived = true;
                entity.IsDeleted = false;
                entity.CreatedTime = DateTime.Now;
                entity.UpdatedTime = DateTime.Now;
                entity.DisplayOrder = 0;
            }
            Entity.AddRange(entities);
            _db.SaveChanges();
        }


/// <summary>
/// 异步批量插入
/// </summary>
/// <param name="entities"></param>
/// <returns></returns>
        public async Task InsertBulkAsync(IEnumerable<T> entities)
        {
            //遍历提供的实体对象集合
            foreach (var entity in entities)
            {
                entity.IsActived = true;
                entity.IsDeleted = false;
                entity.CreatedTime = DateTime.Now;
                entity.UpdatedTime = DateTime.Now;
                entity.DisplayOrder = 0;
            }
            await Entity.AddRangeAsync(entities);
            await _db.SaveChangesAsync();
        }


/// <summary>
/// 修改
/// </summary>
/// <param name="entity"></param>
        public void Update(T entity)
        {
            if (entity == null)
            {
                //entity为null时抛出异常
                throw new ArgumentNullException(nameof(entity));
            }

            entity.UpdatedTime = DateTime.Now;
            _db.SaveChanges();
        }


/// <summary>
/// 批量修改
/// </summary>
/// <param name="entities"></param>
        public void UpdateBulk(IEnumerable<T> entities)
        {
            foreach (var entity in entities)
            {
                entity.UpdatedTime = DateTime.Now;
            }

            Entity.UpdateRange(entities);
            _db.SaveChanges();
        }
    }
}